
#ifndef __SP_IOHANDLER_H_INCLUDED__
#define __SP_IOHANDLER_H_INCLUDED__

class SessionPool;
class Acceptor;
class Session;
class SessionList;
class NetworkObject;

typedef NetworkObject* (*fnCallbackCreateAcceptedObject)();
typedef void (*fnCallbackDestoryAcceptedObject)(NetworkObject*);

class IoHandler
{
	public:
		IoHandler();

		bool Init(size_t nIoThreads, size_t nMaxAcceptSession, size_t nMaxPacket,
			fnCallbackCreateAcceptedObject cbCreateObject, fnCallbackDestoryAcceptedObject cbDestoryObject);

		bool startListen(const char* szIp, int port);

		Session* AllocAcceptSession();

		SessionList* GetAcceptedSessionList() { return pAcceptedSessionList;}

		int GetIoSystemHandler() { return kq;}

	private:

		bool RegisterSession(Session* pSession);
		
		bool ReuseSession(Session* pSession);

public:
	void Update();

	void ProcessAcceptedSessionList();

	void ProcessActiveSessionList();

	void KickDeadSessions();
	private:
		int kq;
		Acceptor* pAcceptor;
		SessionList* pAcceptedSessionList;
		SessionList* pActiveSessionList;
		SessionList* pTmpSessionList;
		SessionPool* pAcceptSessionPool;

		size_t nMaxPacketSize;

		fnCallbackCreateAcceptedObject cbCreate;
		fnCallbackDestoryAcceptedObject cbDestory;

		thread ioThreads[8]; 

		thread sendThread;
};

#endif
